var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var pay = require('../../../services/pay.js');

Page({
  data: {
    
 
    queryBeans: {},
    currentWordNumber:'',
    textArea:'',
    max:150,
    isAgree:false
  },
  orderId: '',
  onLoad: function (options) {

    var that = this;
    // 页面初始化 options为页面跳转所带来的参数status
    console.log(options)
    if(options.orderId){
      that.orderId = options.orderId
    }else{
      console.log("订单id为空")
      wx.showModal({
        content: '没有找到订单信息！',
        showCancel:false,
        success:function(res){
          wx.navigateBack()
          
        }
      })
    }
    that.getOrderDetail()
  },

  getOrderDetail: function () {
    var that = this;

    util.request(api.OrderDetail + that.orderId).then(function (res) {
      console.log("订单详情返回值")
      console.log(res);
      that.setData({
        queryBeans: res.responseBody,
      });
     
    });
  },
  bindTextAreaInput: function (e) {

    // 获取输入框的内容
    var value = e.detail.value;

    // 获取输入框内容的长度
    var len = parseInt(value.length);
    // console.log('当前输入长度：' + len)

    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      currentWordNumber: len, //当前字数  
      textArea: value
    });


  },
  // 申请退款
  applyrefund:function(res){

    var that = this;
    if (that.data.textArea == ''){
      wx.showModal({
        title: '温馨提示',
        content: '请输入退款原因',
        showCancel:false
      })
      return;
    }

    if(!that.data.isAgree){
      wx.showModal({
        title:'温馨提示',
        content: '需要阅读并同意《超途科技退款规则》',
        showCancel: false
      })
      return;
    }

    
    var data={}
    data.orderId = that.orderId
    data.refundReason = that.data.textArea


    util.request(api.ApplyRefund,data,'POST').then(function(res){

        console.log(res)

        if(res.code == 200){
          wx.showModal({
            content: '退款申请已提交，我们将在7个工作日之内退回至您的原支付账户',
            showCancel:false,
            success:function(res){
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=0',
              })
            }
          })
        }else{
          wx.showModal({
            content: '退款申请失败，请联系客服处理',
            showCancel:false
          })
        }

    })


  },

  //取消/选中规则
  cancelOrAgreeRule:function(){

    
    if(this.data.isAgree){
      this.setData({
        isAgree: false
      })
    }else{
      this.setData({
        isAgree: true
      })
      this.refundRuleDetail()
    }
    
  },
  //退款规则详情
  refundRuleDetail: function(){

    var that = this;
    util.request(api.ConfRequest, { type: 'ORDER_RULE_DETAIL' }).then(function (res) {
      console.log(res)
      
      if ('200' == res.code) {
        var memo = res.responseBody

        if(memo.length>=1){
          wx.showModal({
            title: memo[0].key,
            content: memo[0].value,
            showCancel:false
          })
        }
        
      }
    });

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})